//
//  DHHomeExploreView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/27.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeExploreView.h"
#import "FashionArticle.h"
#import "DHFashionWear_LargeRectangleCell.h"
#import "DHFshionWear_SingleCell.h"
#import "DHNewsDetailController.h"
#import "CartButton.h"
#import "DHShoppingCartViewController.h"

@interface DHHomeExploreView()<UITableViewDelegate, UITableViewDataSource>

@property (assign, nonatomic) NSInteger pageIndex;
@property (assign, nonatomic) NSInteger *pageSize;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *articlesArray;

@end

@implementation DHHomeExploreView

- (NSMutableArray *)articlesArray{
    if (!_articlesArray) {
        _articlesArray = [NSMutableArray array];
    }
    return _articlesArray;
}

+ (DHHomeExploreView *)shareDHHomeExploreView{
    DHHomeExploreView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHHomeExploreView class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)initView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHFashionWear_LargeRectangleCell" bundle:nil] forCellReuseIdentifier:kDHFashionWear_LargeRectangleCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHFshionWear_SingleCell" bundle:nil] forCellReuseIdentifier:kDHFshionWear_SingleCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadArticlesData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreArticlesData)];
    [self loadArticlesData];
}

- (void)loadArticlesData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_articlesPageIndex:@(self.pageIndex) pageSize:@(20) andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.articlesArray removeAllObjects];
        FashionArticleList *fashionArticleLsit = [FashionArticleList modelWithDictionary:data];
        [self.articlesArray addObjectsFromArray:fashionArticleLsit.articles];
        [self.tableView reloadData];
        if (self.articlesArray.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        self.pageIndex += 1;
    }];
}

- (void)loadMoreArticlesData{
    [[DHNetAPIClient shareDHNetAPIClient] request_articlesPageIndex:@(self.pageIndex) pageSize:@(20) andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        FashionArticleList *fashionArticleLsit = [FashionArticleList modelWithDictionary:data];
        [self.articlesArray addObjectsFromArray:fashionArticleLsit.articles];
        self.pageIndex += 1;
        [self.tableView reloadData];
        if (fashionArticleLsit.articles.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.articlesArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FashionArticle *article = self.articlesArray[indexPath.row];
    if ([article.layout isEqualToString:@"单行列表"]) {
        return 120;
    }if ([article.layout isEqualToString:@"大矩形"]) {
        return 304;
    }else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FashionArticle *article = self.articlesArray[indexPath.row];
    if ([article.layout isEqualToString:@"单行列表"]) {
        DHFshionWear_SingleCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHFshionWear_SingleCell forIndexPath:indexPath];
        [cell updateWith:article];
        return cell;
    }if ([article.layout isEqualToString:@"大矩形"]) {
        DHFashionWear_LargeRectangleCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHFashionWear_LargeRectangleCell forIndexPath:indexPath];
        [cell updateWith:article];
        return cell;
    }else{
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FashionArticle *article = self.articlesArray[indexPath.row];
    DHNewsDetailController *adController = [[DHNewsDetailController alloc] initWithArticleID:article._id];
    [self.viewController.navigationController pushViewController:adController animated:YES];
}

@end
